package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.app.domain.AppUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppUserCodeBo;
import com.ruoyi.app.domain.vo.AppUserCodeVo;
import com.ruoyi.app.domain.AppUserCode;
import com.ruoyi.app.mapper.AppUserCodeMapper;
import com.ruoyi.app.service.IAppUserCodeService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 实名认证Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-08
 */
@RequiredArgsConstructor
@Service
public class AppUserCodeServiceImpl implements IAppUserCodeService {

    private final AppUserCodeMapper baseMapper;

    /**
     * 查询实名认证
     */
    @Override
    public AppUserCodeVo queryById(Long codeId){
        return baseMapper.selectVoById(codeId);
    }

    @Override
    public AppUserCodeVo queryByOrderNo(String orderNo) {
        LambdaQueryWrapper<AppUserCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppUserCode::getOrderNo, orderNo);
        return baseMapper.selectVoOne(wrapper);
    }

    /**
     * 查询实名认证列表
     */
    @Override
    public TableDataInfo<AppUserCodeVo> queryPageList(AppUserCodeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserCode> lqw = buildQueryWrapper(bo);
        Page<AppUserCodeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询实名认证列表
     */
    @Override
    public List<AppUserCodeVo> queryList(AppUserCodeBo bo) {
        LambdaQueryWrapper<AppUserCode> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserCode> buildQueryWrapper(AppUserCodeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserCode> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AppUserCode::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppUserCode::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getVerifyStatus()), AppUserCode::getVerifyStatus, bo.getVerifyStatus());
        return lqw;
    }

    /**
     * 新增实名认证
     */
    @Override
    public Boolean insertByBo(AppUserCodeBo bo) {
        AppUserCode add = BeanUtil.toBean(bo, AppUserCode.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCodeId(add.getCodeId());
        }
        return flag;
    }

    /**
     * 修改实名认证
     */
    @Override
    public Boolean updateByBo(AppUserCodeBo bo) {
        AppUserCode update = BeanUtil.toBean(bo, AppUserCode.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserCode entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除实名认证
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<AppUserCode> selUserCodeList(Long id) {
        LambdaQueryWrapper<AppUserCode> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AppUserCode::getUserId, id);
        return baseMapper.selectList(lqw);
    }

    @Override
    public void updateCodeData(AppUserCode appUserCode) {
        LambdaUpdateWrapper<AppUserCode> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(AppUserCode::getCodeId, appUserCode.getCodeId());
        lqw.set(AppUserCode::getCode, appUserCode.getCode());
        lqw.set(AppUserCode::getCodeName, appUserCode.getCodeName());
        baseMapper.update(appUserCode, lqw);
    }

    @Override
    public Set<Long> selAuthed() {
        Set<Long> set = new HashSet<>();
        LambdaQueryWrapper<AppUserCode> appUserCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appUserCodeLambdaQueryWrapper.eq(AppUserCode::getVerifyStatus, "1");
        List<AppUserCode> appUserCodes = baseMapper.selectList(appUserCodeLambdaQueryWrapper);
        if (appUserCodes != null && appUserCodes.size() > 0) {
            for (AppUserCode appUserCode : appUserCodes) {
                set.add(appUserCode.getUserId());
            }
        }
        return set;
    }

    @Override
    public int queryAuthed() {
        //查询实名认证后的人数
        LambdaQueryWrapper<AppUserCode> appUserCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appUserCodeLambdaQueryWrapper.eq(AppUserCode::getVerifyStatus, 1);
        List<AppUserCode> appUsers = baseMapper.selectList(appUserCodeLambdaQueryWrapper);
        if (appUsers != null){
            Map<Long, List<AppUserCode>> collect = appUsers.stream().collect(Collectors.groupingBy(AppUserCode::getUserId));
            return collect.size();
        }

        return 0;
    }
}
